import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.Timer;
public class MyGame extends JFrame {
    public MyGame() {
        GameBody gamebody = new GameBody();
        gamebody.setBounds(5, 10, 500, 600);  //
        gamebody.setOpaque(false);
        gamebody.setLayout(null);
        addKeyListener(gamebody);
        add(gamebody);

        int w = Toolkit.getDefaultToolkit().getScreenSize().width;
        int h = Toolkit.getDefaultToolkit().getScreenSize().height;
        final JButton login = new JButton(new ImageIcon("image/cxks.png"));
        login.setContentAreaFilled(false);
        login.setMargin(new Insets(0, 0, 0, 0));
        login.setBorderPainted(false);
        login.setBounds(340, 320, 120, 26);
        gamebody.add(login);

        login.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {    //登录的按钮
                if (e.getSource() == login) {
                    requestFocus(true);    //获得焦点，不用失去焦点
                    gamebody.resetMap();   //重置地图
                    gamebody.drawWall();   //冲重新绘制边界墙体
                    gamebody.createshape();   //重新产生新的地图
                    gamebody.setStart(false);  //唤醒定时下落的线程
                    gamebody.score = 0;       //将分数置为零
                    repaint();
                }
            }
        });

        final JButton pauselogin = new JButton(new ImageIcon("image/zt.png"));
        pauselogin.setContentAreaFilled(false);
        pauselogin.setMargin(new Insets(0, 0, 0, 0));
        pauselogin.setBorderPainted(false);
        pauselogin.setBounds(340, 370, 120, 26);
        gamebody.add(pauselogin);

        pauselogin.addMouseListener(new MouseListener() {   //暂停的按钮
            //鼠标点击事件，可以分别判断不同的事件，做出不同的反应
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == e.BUTTON1) {     //单击左键暂停
                    gamebody.setStart(true);   //将自动下落线程关闭
                    //requestFocus(true);    //同时整个JFrame失去焦点，无法操作，但可以点击按钮
                } else if (e.getButton() == e.BUTTON3) {   //右击暂停，继续游戏
                    gamebody.setStart(false);   //唤醒自动下落线程
                    requestFocus(true);
                }
           /*     if(e.getClickCount()==2){     //左键双击，也可以继续游戏
                    gamebody.setStart(false);
                    requestFocus(true);
                }*/
            }

            public void mouseEntered(MouseEvent e) {
            }

            public void mouseExited(MouseEvent e) {
            }

            public void mousePressed(MouseEvent e) {
            }

            public void mouseReleased(MouseEvent e) {
            }

        });

        setTitle("俄罗斯方块");
        setResizable(false);
        setFocusable(true);
        setBounds((w - 500) / 2, (h - 600) / 2, 500, 600);
        setLayout(null);

        setVisible(true);
        setDefaultCloseOperation(3);
    }

    public static void main(String[] args) {
        new MyGame();
    }

}